package x_yeyue;


import x_yeyue.obj.*;
import x_yeyue.utils.GameUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class GameWin extends JFrame {
    // 游戏状态
    // 0未开始  1游戏中  2暂停  3失败  4通关
    public static int state = 0;

    BgObj bgobj = new BgObj(GameUtils.bgImg, 0, -1800, 2);
    // 我方飞机对象
    PlaneObj planeObj = new PlaneObj(GameUtils.planeImg, 37, 41, 290, 550, 0, this);
    // 获取敌方boss对象
    BossObj bossObj = new BossObj(GameUtils.bossImg, 240, 174, 180, -180, 3, this);
    // littleboss1
    LittleBoss1 littleBoss1 = new LittleBoss1(GameUtils.littleboss1Img, 172, 112, 300, -150, 2, this);
    // 获取预警标志对象
    WaringObj waringObj = new WaringObj(GameUtils.warningImg, 599, 90, 0, 350, 0, this);


    // 定义一个图片变量
    Image offScreenImage = null;
    // 记录游戏绘制的次数
    int count = 1;
    // 计分变量
    public static int score = 0;
    // 我方飞机的索引
    public static int planeindex = 0;


    public void launch(){
        // 窗口是否可见
        this.setVisible(true);
        // 窗口大小
        this.setSize(600, 800);
        // 窗口位置
        this.setLocationRelativeTo(null);
        // 窗口标题
        this.setTitle("雷霆战机2.0");
        // 关闭窗口会自动关闭进程
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 将所有要绘制的游戏物体全部放入到游戏集合中
        GameUtils.gameObjList.add(bgobj);
        GameUtils.gameObjList.add(planeObj);
        // 获取我方飞机的索引值
        planeindex = GameUtils.gameObjList.indexOf(planeObj);


        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getButton() == 1 && state == 0){
                    state = 1;
//                    repaint();

                }
            }
        } );

        // 添加键盘监听事件
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode() == 32){
                    if(state == 1) {
                        state = 2;
                    }else if(state == 2){
                        state = 1;
                    }
                }
            }
        });

        while (true){
            creatObj();
            repaint();
            try{
                Thread.sleep(25);
            } catch (InterruptedException e){
                e.printStackTrace();
            }

        }
    }

    @Override
    public void paint(Graphics g) {
        if(offScreenImage == null){
            offScreenImage = createImage(600, 800);
        }
        Graphics gImage = offScreenImage.getGraphics();
        gImage.fillRect(0, 0, 600, 800);
        // 游戏未开始
        if(state == 0){
            gImage.drawImage(GameUtils.bgImg, 0, 0, null);
            gImage.drawImage(GameUtils.bossImg, 190, 70, null);
            gImage.drawImage(GameUtils.explodeImg, 270, 350, null);
            gImage.drawImage(GameUtils.planeImg, 280, 470, null);

            // 绘制文字
            gImage.setColor(Color.WHITE);
            gImage.setFont(new Font("仿宋", Font.BOLD, 30));
            gImage.drawString("鼠标左键开始游戏", 180, 300);
        }
        // 游戏开始
        if(state == 1){
            GameUtils.gameObjList.addAll(GameUtils.explodeObjsList);
            for(int i = 0; i < GameUtils.gameObjList.size(); i ++){
                GameUtils.gameObjList.get(i).paintSelf(gImage);
            }
            GameUtils.gameObjList.removeAll(GameUtils.removeList);

//            if(count % 100 == 0)GameUtils.removeList.clear();



        }
        // 游戏暂停
        if(state == 2){
            gImage.drawImage(GameUtils.bgImg, 0, 0, null);
            GameUtils.drawWord(gImage, "游戏暂停", Color.YELLOW, 30, 220, 300);
        }
        // 游戏失败
        if(state == 3){
            gImage.drawImage(GameUtils.bgImg, 0, 0, null);
            GameUtils.drawWord(gImage, "游戏失败", Color.RED, 30, 220, 300);
        }
        // 游戏通关
        if(state == 4){
            gImage.drawImage(GameUtils.bgImg, 0, 0, null);
            GameUtils.drawWord(gImage, "游戏通关", Color.YELLOW, 30, 220, 300);
        }
        GameUtils.drawWord(gImage, score + "分", Color.GREEN, 40, 30, 100);
        GameUtils.drawWord(gImage, PlaneObj.health + "血量", Color.GREEN, 30, 30, 750);
        g.drawImage(offScreenImage, 0, 0, null);
        count ++;
    }

    void creatObj(){
        // 子弹
        if(count % (Math.max(15 - PlaneObj.giftCount, 1)) == 0){
            if(PlaneObj.giftCount >= 3 && PlaneObj.bossGiftCount == 0){
                ShellObj shellObj1 = new ShellObj(GameUtils.shell2Img, 44, 67, planeObj.getX() + 12, planeObj.getY() - 20, 5, this);
                ShellObj shellObj2 = new ShellObj(GameUtils.shell2Img, 44, 67, planeObj.getX() - 24, planeObj.getY() - 20, 5, this);
                // 添加新子弹到子弹集合中
                GameUtils.shellObjList.add(shellObj1);
                GameUtils.shellObjList.add(shellObj2);
                // 添加新子弹到游戏对象中
                GameUtils.gameObjList.add(shellObj1);
                GameUtils.gameObjList.add(shellObj2);
            }else if(PlaneObj.giftCount >= 3 && PlaneObj.bossGiftCount > 0){
                ShellObj shellObj1 = new ShellObj(GameUtils.shell3Img, 44, 67, planeObj.getX() + 12, planeObj.getY() - 20, 5, this);
                ShellObj shellObj2 = new ShellObj(GameUtils.shell3Img, 44, 67, planeObj.getX() - 40, planeObj.getY() - 20, 5, this);
                ShellObj shellObj3 = new ShellObj(GameUtils.shell3Img, 44, 67, planeObj.getX() + 80, planeObj.getY() - 20, 5, this);
                // 添加新子弹到子弹集合中
                GameUtils.shellObjList.add(shellObj1);
                GameUtils.shellObjList.add(shellObj2);
                GameUtils.shellObjList.add(shellObj3);
                // 添加新子弹到游戏对象中
                GameUtils.gameObjList.add(shellObj1);
                GameUtils.gameObjList.add(shellObj2);
                GameUtils.gameObjList.add(shellObj3);
            } else{
                // 实例化一颗新子弹
                ShellObj shellObj = new ShellObj(GameUtils.shellImg, 14, 29, planeObj.getX() + 12, planeObj.getY() - 20, 5, this);
                // 添加新子弹到子弹集合中
                GameUtils.shellObjList.add(shellObj);
                // 添加新子弹到游戏对象中
                GameUtils.gameObjList.add(shellObj);
            }

        }
        // 敌方小飞机
        if(count % 15 == 0){
            int x = (int) ((Math.random() * 10) * 60);
            // 实例化一个敌方飞机
            Enemy1Obj enemy1Obj = new Enemy1Obj(GameUtils.enemy1Img, 32, 24, x, 0, 5.0, this);

            // 添加新子弹到子弹集合中
            GameUtils.enemy1ObjsList.add(enemy1Obj);
            // 添加新子弹到游戏对象中
            GameUtils.gameObjList.add(enemy1Obj);
        }
        // 生成预警
        if(count == 1050){
            GameUtils.gameObjList.add(waringObj);
        }
        if(count == 1100){
            GameUtils.removeList.add(waringObj);
        }

        // 生成Boss
        if(count == 1200){
            GameUtils.gameObjList.add(bossObj);
        }
        if(count == 250){
            GameUtils.gameObjList.add(littleBoss1);
        }
        if(count % 20 == 0){
            if(GameUtils.gameObjList.contains(bossObj)){
                BossBullet bossBullet = new BossBullet(GameUtils.bossBulletImg, 91, 90, bossObj.getX() + 70, bossObj.getY() + 100, 9, this);
                // 添加boss子弹添加到集合中
                GameUtils.bossBulletList.add(bossBullet);
                // 添加新子弹到游戏对象中
                GameUtils.gameObjList.add(bossBullet);
            }
        }
        // 敌方大飞机
        if(count % 25 == 0){
            if(count % 100 == 0){
                int x = (int) ((Math.random() * 10) * 60);
                // 实例化一个敌方飞机
                Enemy2Obj enemy2Obj = new Enemy2Obj(GameUtils.enemy2Img, 44, 67, x, 0, 3, this);
                // 添加新子弹到子弹集合中
                GameUtils.enemy2ObjList.add(enemy2Obj);
                // 添加新子弹到游戏对象中
                GameUtils.gameObjList.add(enemy2Obj);
            }
            if(!GameUtils.enemy2ObjList.isEmpty()){
                // 获得敌方大飞机的 x 轴和 y 轴
                Enemy2Obj enemy2Obj = GameUtils.enemy2ObjList.get(GameUtils.enemy2ObjList.size() - 1);
                int x = enemy2Obj.getX();
                int y = enemy2Obj.getY();
                Enemy2BulletObj enemy2BulletObj = new Enemy2BulletObj(GameUtils.enemy2BulletImg, 14, 25, x + 17, y + 15, 6, this);
                GameUtils.enemy2BulletObjList.add(enemy2BulletObj);
                GameUtils.gameObjList.add(enemy2BulletObj);
            }
        }
        // 敌方boss1
        if(count % 40 == 0){
            if(GameUtils.gameObjList.contains(littleBoss1)){
                LittleBoss1Bullet littleBoss1Bullet = new LittleBoss1Bullet(GameUtils.littleboss1BulletImg, 21, 59, littleBoss1.getX() + 78, littleBoss1.getY() + 100, 8, this);
                GameUtils.littleBoss1BulletList.add(littleBoss1Bullet);
                GameUtils.gameObjList.add(littleBoss1Bullet);
            }
        }
    }

    public static void main(String[] args) {
        GameWin gameWin = new GameWin();
        gameWin.launch();
    }
}
